< previous page page_360 next page >

Page 360
Object-oriented programming techniques are useful because they allow you to reuse your code efficiently. This chapter discusses how to create classes in Visual Basic. Classes allow you to create objects and take advantage of object-oriented programming techniques in Visual Basic. When working on a large project, for example, each team member can create a specific class and compile it into an ActiveX DLL for others to use.
Understanding Classes
If you have used custom controls in a Visual Basic program, you have already used classes and objects. For example, when you draw a text box on a form, you are actually creating a specific instance of the text box class. For example, if you draw five text boxes on your form, you have created five instances of the text box class. Even though each instance is a distinct entity, they were all created from the same template.
Instances of a class are known as objects. Each different class is a template from which a specific type of object is created. In this example, the text box class defines that a text box has a Text property. However, the class definition itself does not contain information about the values of the properties. Instead, an object you create from the class, for example, txtLastName, actually contains information.
Object-Oriented Programming
You probably have heard the term object-oriented programming (OOP) or read about it in programming books and magazines. A key element of OOP is its use of reusable objects to build programs.
OOP begins in the design stage, when you determine the objects in an application. For example, suppose you have to write a system to manage paychecks for employees. A traditional design plan would be to determine each program function, such as "Adding an employee to the database" or "Printing an employee paycheck." An object-oriented design would instead try to separate programming tasks along the lines of the objects in the program (employees, database, paycheck, and so on). In order for a design to be considered object-oriented, several facts must be true about the objects. These fundamental concepts of OOP are summarized in the following list:
Encapsulation. Encapsulation, or information hiding, refers to the fact that objects hide the details of how they work. For example, when you set the Text property of a text box, you do not know (or care) how the text box internally repaints the characters. Information hiding allows the programmer of an object to change how an object works without affecting the users of the object.
Inheritance. A new object can be defined based on an existing object, and it can contain all the same properties and methods. For example, you can create a new object that contains all the standard properties and methods of an existing object plus a few of your own. You can just add your own extra properties and "inherit" the existing ones. Visual Basic, strictly speaking, does not support inheritance.

 
< previous page page_360 next page >